using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Infrastructure
{
    /// <summary>
    /// 枚举量辅助类
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 根据名称获取枚举值
        /// </summary>
        /// <param name="enumName">枚举量的名称</param>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns>枚举值</returns>
        public static T GetEnumValue<T>(string enumName) where T : Enum
        {
            try
            {
                Type enumType = typeof(T);

                // 得到枚举类型的所有值
                var values = Enum.GetValues(enumType);

                var ht = new Hashtable();

                foreach (var val in values)
                {
                    ht.Add(Enum.GetName(enumType, val), val);
                }

                // 返回枚举值
                return (T)ht[enumName];
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 将枚举转换成字典集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> GetEnumDic<T>()
        {

            Dictionary<string, int> resultList = new Dictionary<string, int>();
            Type type = typeof(T);
            var strList = Enum.GetNames(typeof(T)).ToList();
            foreach (string key in strList)
            {
                string val = Enum.Format(type, Enum.Parse(type, key), "d");
                resultList.Add(key, int.Parse(val));
            }
            return resultList;
        }

        /// <summary>
        /// 根据枚举的值获取枚举名称
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="status">枚举的值</param>
        /// <returns></returns>
        public static string GetEnumName<T>(this int status)
        {
            return Enum.GetName(typeof(T), status);
        }

        /// <summary>
        /// 将枚举转为集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumEntity> EnumToList<T>()
        {
            List<EnumEntity> list = new List<EnumEntity>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumEntity m = new EnumEntity();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    System.ComponentModel.DescriptionAttribute da = objArr[0] as System.ComponentModel.DescriptionAttribute;
                    m.Desction = da.Description;
                }
                m.EnumValue = Convert.ToInt32(e);
                m.EnumName = e.ToString();
                list.Add(m);
            }
            return list;
        }
    }

    /// <summary>
    /// 枚举实体
    /// </summary>
    public class EnumEntity
    {
        /// <summary>  
        /// 枚举的描述  
        /// </summary>  
        public string Desction { set; get; }

        /// <summary>  
        /// 枚举名称  
        /// </summary>  
        public string EnumName { set; get; }

        /// <summary>  
        /// 枚举对象的值  
        /// </summary>  
        public int EnumValue { set; get; }
    }
}
